package com.ddf.publics.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.ddf.publics.tool.MessageCode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

public class SmsConfig {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            SmsConfig smsUtils = new SmsConfig();
            String telphoneString = "手机号";
            smsUtils.sendSms(telphoneString);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    //短信API产品名称
    static final String product="Dysmsapi";
    //短信API产品域名
    static final String domain="dysmsapi.aliyuncs.com";
    //AccessKey ID
    //private  String accessKeyId;
    ////Access Key
    //private   String accessKeySecret;

    //随机验证码
    private static int code;
    private static String sfcg;
    public static String getSfcg() {
        return sfcg;
    }
    public static void setSfcg(String sfcg) {
        SmsConfig.sfcg = sfcg;
    }
    public static int getCode(){
        return code;
    }
    public static void setCode(){
        while (true) {
            code = (int)(Math.random()*999999)+100000; //每次调用生成一次四位数的随机数
            if(code>999999)
            {
                code = (int)(Math.random()*999999)+100000; //每次调用生成一次四位数的随机数
            }else{
                return ;
            }
        }


    }

    public SendSmsResponse sendSms(String telphone) throws ClientException {
        //设置超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化ascClient
        IClientProfile profile= DefaultProfile.getProfile("cn-qingdao", MessageCode.accessKeyId, MessageCode.accessKeySecret);
        DefaultProfile.addEndpoint("cn-qingdao", "cn-qingdao",product, domain);
        IAcsClient acsClient=new DefaultAcsClient(profile);
        //组装请求对象
        SendSmsRequest request=new SendSmsRequest();
        //使用post提交
        request.setMethod(MethodType.POST);
        //待发送的手机号
        request.setPhoneNumbers(telphone);
        //短信签名
        request.setSignName("卡戎图书");
        //短信模板ID
        request.setTemplateCode("SMS_192825698");
        //验证码
        SmsConfig sms = new SmsConfig();
        sms.setCode();
        String codetemp = sms.getCode()+"";
        System.out.println("code:        "+codetemp);

        /*
         * 可选:模板中的变量替换JSON串,
         * 如模板内容为"亲爱的${name},您的验证码为${code}"时,
         * 此处的值为{"name":"Tom","code":"1454"}
         *   \  反斜杠为转义字符，使得输出双引号
         */
        request.setTemplateParam("{\"name\":\"Tom\", \"code\":\""+codetemp+"\"}");
        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        //request.setOutId("1454");
        SendSmsResponse response=acsClient.getAcsResponse(request);

        if(response.getCode() != null && response.getCode().equals("OK")) {
            //请求成功
            sms.setSfcg("0");

        }else {
            sms.setSfcg("1");
        }
        return response;
    }
}
